﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;

namespace LevenBlog.Core.Service
{
    public static class ImageType
    {
        private static IList<lb_imagetype> imagetypeList = null;
        private static object lockObject = new object();

        public static void Load(IDbProvider provider)
        {
            lock (lockObject)
            {
                imagetypeList = (ProviderManager.Factory.ImageType.Get(provider) as List<lb_imagetype>).AsReadOnly();
            }
        }

        public static IList<lb_imagetype> Get(IDbProvider provider)
        {
            if (imagetypeList == null)
                Load(provider);
            return imagetypeList;
        }

        public static lb_imagetype Get(IDbProvider provider, int id)
        {
            try
            {
                return ProviderManager.Factory.ImageType.Get(provider, id);
            }
            catch
            {
                return null;
            }
        }

        public static void Update(IDbProvider provider, lb_imagetype imagetype)
        {
            ProviderManager.Factory.ImageType.Update(provider, imagetype);
            Load(provider);
        }

        public static void Delete(IDbProvider provider, int id)
        {
            provider.BeginTransaction();
            try
            {
                ProviderManager.Factory.ImageType.Delete(provider, id);
                ProviderManager.Factory.Image.DeleteFromTypeID(provider, id);
                Load(provider);
                provider.Commit();
            }
            catch
            {
                provider.Rollback();
                throw;
            }
        }

        public static void Insert(IDbProvider provider, lb_imagetype imagetype)
        {
            ProviderManager.Factory.ImageType.Insert(provider, imagetype);
            Load(provider);
        }

        public static void Remove()
        {
            imagetypeList = null;
        }
    }
}
